package com.qili.service;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qili.annotation.Mean;
import com.qili.base.mapper.BaseMapper;
import com.qili.base.service.impl.BaseServiceImpl;
import com.qili.core.utils.DictTypes;
import com.qili.core.utils.DictUtil;
import com.qili.entity.affaircase.dto.AffairCaseDTO;
import com.qili.entity.affaircase.AffairCase;
import com.qili.entity.base.BaseDict;
import com.qili.entity.supervisor.ProjectNotice;
import com.qili.entity.supervisor.SupervisorFileTemp;
import com.qili.entity.supervisor.SupervisorProjectModifiedRecord;
import com.qili.entity.supplier.*;
import com.qili.entity.supplier.dto.ProjectPlaceChangeDTO;
import com.qili.entity.supplier.dto.ProjectPlaceDTO;
import com.qili.entity.supplier.dto.SupplierProjectChangeDTO;
import com.qili.entity.supplier.dto.SupplierProjectDTO;
import com.qili.entity.sys.SysRegion;
import com.qili.exception.MyException;
import com.qili.mapper.affairCase.AffairCaseMapper;
import com.qili.mapper.supervisor.SupervisorProjectMapper;
import com.qili.service.base.BaseDictService;
import com.qili.service.supervisor.ProjectNoticeServie;
import com.qili.service.supervisor.SupervisorFileTempService;
import com.qili.service.supervisor.SupervisorProjectModifiedRecordService;
import com.qili.service.supplier.*;
import com.qili.service.sys.SysRegionService;
import com.qili.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author zhaoyongke
 * @date 2020/11/12
 * @description
 */
@Service
public class AffairService  extends BaseServiceImpl<AffairCase, String> {
    @Autowired
    AffairCaseMapper  affairCaseMapper;
    @Autowired
    ProjectPlaceChangeService projectPlaceChangeService;

    @Autowired
    SupervisorFileTempService supervisorFileTempService;

    @Autowired
    BaseDictService baseDictService;
    @Autowired
    SysRegionService sysRegionService;
    @Autowired
    ProjectPlaceService projectPlaceService;
    @Autowired
    SupplierProjectManagerService supplierProjectManagerService;
    @Autowired
    SupplierProjectManagerChangeService supplierProjectManagerChangeService;
    @Autowired
    SupplierProjectAcceptService supplierProjectAcceptService;

    @Autowired
    SupervisorProjectModifiedRecordService supervisorProjectModifiedRecordService;
    @Autowired
    ProjectNoticeServie projectNoticeServie;
    @Autowired
    SupervisorProjectMapper supervisorProjectMapper;
    @Override
    public BaseMapper<AffairCase, String> getMappser() {
        return affairCaseMapper;
    }
    /**
    * @Author:zhaoyongke
    * @Description: 查询代码事项
    * @param dto
    * @Date:11:18 2020/11/14
    */
    public List<AffairCaseDTO> queryMyTodoSh(AffairCaseDTO dto){
        List<AffairCaseDTO>  list=   affairCaseMapper.queryMyTodoSh(dto);
        return  list;
    }


    public List<AffairCaseDTO> queryMyTodoShApp(AffairCaseDTO dto){
        List<AffairCaseDTO>  list=   affairCaseMapper.queryMyTodoShApp(dto);
        return  list;
    }


    /**
    * @Author:zhaoyongke
    * @Description: 查询已办理
    * @param dto
    * @Date:16:19 2020/12/8
    */
    public List<AffairCaseDTO> queryMyHisSh(AffairCaseDTO dto){
        List<AffairCaseDTO>  list=   affairCaseMapper.queryMyHisSh(dto);
        return  list;
    }
    /**
    * @Author:zhaoyongke
    * @Description: 查询发起的
    * @param dto
    * @Date:15:31 2020/12/11
    */
    public List<AffairCaseDTO> queryMyFqCase(AffairCaseDTO dto){
        List<AffairCaseDTO>  list=   affairCaseMapper.queryMyFqCase(dto);
        return  list;
    }
    public List<AffairCaseDTO> queryMyFqCaseApp(AffairCaseDTO dto){
        List<AffairCaseDTO>  list=   affairCaseMapper.queryMyFqCaseApp(dto);
        return  list;
    }
    /**
    * @Author:zhaoyongke
    * @Description: 办结
    * @param dto
    * @Date:15:22 2021/1/26
    */
    public List<AffairCaseDTO> queryBjCase(AffairCaseDTO dto){
        List<AffairCaseDTO>  list=   affairCaseMapper.queryBjCase(dto);
        return  list;
    }

    /**
     * @Author:zhaoyongke
     * @Description:
     * @param dto
     * @Date:20:53 2021/1/17
     */
    public List<AffairCaseDTO> queryMyNotice(AffairCaseDTO dto){
        List<AffairCaseDTO> list= affairCaseMapper.queryMyNotice(dto);
        return list;
    }


    public List<AffairCaseDTO> queryMyNoticeApp(AffairCaseDTO dto){
        List<AffairCaseDTO> list= affairCaseMapper.queryMyNoticeApp(dto);
        return list;
    }


    /**
     * @Author:zhaoyongke
     * @Description: 查询待办理 分页
     * @param dto
     * @Date:16:19 2020/12/8
     */
    public ReType queryMyTodoShList(AffairCaseDTO dto, int page, int limit){
        List<AffairCaseDTO> tList = null;
        Page<AffairCaseDTO> tPage = PageHelper.startPage(page, limit);
        try {
            tList = affairCaseMapper.queryMyTodoSh(dto);
        } catch (MyException e) {
            e.printStackTrace();
        }
        return new ReType(tPage.getTotal(), tList);
    }
    /**
    * @Author:zhaoyongke
    * @Description:查询已办理 分页
    * @param dto
    * @param page
    * @param limit
    * @Date:18:56 2020/12/8
    */
    public ReType queryMyHisShList(AffairCaseDTO dto, int page, int limit){
        List<AffairCaseDTO> tList = null;
        Page<AffairCaseDTO> tPage = PageHelper.startPage(page, limit);
        try {
            tList = affairCaseMapper.queryMyHisSh(dto);
        } catch (MyException e) {
            e.printStackTrace();
        }
        return new ReType(tPage.getTotal(), tList);
    }
   /**
   * @Author:zhaoyongke
   * @Description: 我发起的流程
   * @param dto
    * @param page
    * @param limit
   * @Date:15:29 2020/12/11
   */
    public ReType queryMyFqCase(AffairCaseDTO dto, int page, int limit){
        List<AffairCaseDTO> tList = null;
        Page<AffairCaseDTO> tPage = PageHelper.startPage(page, limit);
        try {
            tList=affairCaseMapper.queryMyFqCase(dto);
        } catch (MyException e) {
            e.printStackTrace();
        }
        return new ReType(tPage.getTotal(), tList);
    }
    /**
    * @Author:zhaoyongke
    * @Description: 办结流程
    * @param dto
    * @param page
    * @param limit
    * @Date:15:20 2021/1/26
    */
    public ReType queryBjCase(AffairCaseDTO dto, int page, int limit){
        List<AffairCaseDTO> tList = null;
        Page<AffairCaseDTO> tPage = PageHelper.startPage(page, limit);
        try {
            tList=affairCaseMapper.queryBjCase(dto);
        } catch (MyException e) {
            e.printStackTrace();
        }
        return new ReType(tPage.getTotal(), tList);
    }


    /**
    * @Author:zhaoyongke
    * @Description: 审批进程
    * @param dto
    * @param page
    * @param limit
    * @Date:17:26 2020/12/11
    */
    public ReType queryProcessList(AffairCaseDTO dto, int page, int limit){
        List<AffairCaseDTO> tList = null;
        Page<AffairCaseDTO> tPage = PageHelper.startPage(page, limit);
        try {
            tList=affairCaseMapper.queryProcessList(dto);
        } catch (MyException e) {
            e.printStackTrace();
        }
        return new ReType(tPage.getTotal(), tList);
    }
    /**
    * @Author:zhaoyongke
    * @Description: 查询供需没有审核的发布项目
    * @param dto
    * @Date:18:06 2021/4/19
    */
    public  List<AffairCaseDTO> queryTodoGxShCheck(AffairCaseDTO dto){
      return  affairCaseMapper.queryTodoGxShCheck(dto);
    }
    public  List<AffairCaseDTO> queryTodoGxShZdCheck(AffairCaseDTO dto){
        return  affairCaseMapper.queryTodoGxShZdCheck(dto);
    }
    public  List<AffairCaseDTO> queryAruTask(AffairCaseDTO dto){
        return  affairCaseMapper.queryAruTask(dto);
    }






    /**
     * @param checkStatus
     * @param project
     * @Author:zhaoyongke
     * @Description:供需 ---审核后处理
     * @Date:17:38 2021/3/29
     */
    public void updateGxChangeProject(String checkStatus, SupplierProject project,String  type) throws Exception {
        SupplierProjectDTO supdto = new SupplierProjectDTO();
        supdto.setSupProjectId(project.getSupProjectId());
        SupplierProjectChangeDTO changeProject = supplierProjectManagerChangeService.getChangeProject(supdto);
        String currdate = DateUtil.getCurrentDateString("yyyyMMdd");

        System.out.println("进入时间updateGxChangeProject============2222222============="+project.getProjectName());
        System.out.println("进入时间updateGxChangeProject============888888888888888============="+project);
        if ("2".equals(checkStatus)) {

            System.out.println("进入时间updateGxChangeProject============2222222============="+project);
//            Boolean aBoolean = addChangeRecord(project, changeProject);
//            if (aBoolean) {
                changeProject.setTitle(project.getProjectName() + "项目变更公告（" + currdate + "）版");
                changeProject.setChangeDate(DateUtil.getCurrentDateString("yyyy-MM-dd HH:mm"));
                project.setChangeDate(DateUtil.getCurrentDateString("yyyy-MM-dd HH:mm"));
                sendNoticeToYjByChange(project, changeProject);
//            }
            String createBy = project.getCreateBy();
            String createDate = project.getCreateDate();
            String publishBy = project.getPublishBy();
            String publishType = project.getPublishType();
            BeanUtil.copyNotNullBean(changeProject, project);
            ProjectPlace placeDTO = new ProjectPlace();
            placeDTO.setSupProjectId(project.getSupProjectId());
            projectPlaceService.delete(placeDTO);
            //实施地点查询
            ProjectPlaceChangeDTO placeChangeDTO = new ProjectPlaceChangeDTO();
            placeChangeDTO.setSupProjectChangeId(changeProject.getSupProjectChangeId());
            placeChangeDTO.setSupProjectId(project.getSupProjectId());
            List<ProjectPlaceChange> placeList = projectPlaceChangeService.select(placeChangeDTO);
            for (int i = 0; i < placeList.size(); i++) {
                ProjectPlaceChange projectPlaceChange = placeList.get(i);
                ProjectPlace place = new ProjectPlace();
                place.setType(projectPlaceChange.getType());// 001 实施地点 002 人员要求地点
                place.setSupProjectId(project.getSupProjectId());
                place.setProvince(projectPlaceChange.getProvince());
                place.setCity(projectPlaceChange.getCity());
                place.setCounty(projectPlaceChange.getCounty());
                projectPlaceService.insertSelective(place);
            }
            // 删除原来的文件
            SupervisorFileTemp temp = new SupervisorFileTemp();
            temp.setLinkId(project.getSupProjectId());
            List<SupervisorFileTemp> select = supervisorFileTempService.select(temp);

            for (int i = 0; i < select.size(); i++) {
                SupervisorFileTemp supervisorFileTemp = select.get(i);
                supervisorFileTemp.setLinkId("555");
                supervisorFileTempService.updateByPrimaryKey(supervisorFileTemp);
            }
            if(StringUtils.isBlank(changeProject.getSupProjectChangeId())){
                throw  new Exception("变更主键为空");
            }
            // 复制变更的
            SupervisorFileTemp tempcahng = new SupervisorFileTemp();
            tempcahng.setLinkId(changeProject.getSupProjectChangeId());
            List<SupervisorFileTemp> selecttempcahng = supervisorFileTempService.select(tempcahng);
            System.out.println("changeProject============changeProjectbainhaujduijfa============="+changeProject.getSupProjectChangeId()+"kkkkkkkkkkkkkkkk"+selecttempcahng.size());
            for (int i = 0; i < selecttempcahng.size(); i++) {
                SupervisorFileTemp supervisorFileTemp = selecttempcahng.get(i);
                SupervisorFileTemp addtemp = new SupervisorFileTemp();
                BeanUtil.copyNotNullBean(supervisorFileTemp, addtemp);
                addtemp.setLinkId(project.getSupProjectId());
                addtemp.setId(IdUtil.simpleUUID());
                supervisorFileTempService.insertSelective(addtemp);
            }

            project.setCreateDate(createDate);
            project.setPublishBy(publishBy);
            project.setPublishType(publishType);
            //
        }
        project.setCheckStatus(checkStatus);
        supplierProjectManagerService.updateByPrimaryKey(project);
        // 更新變更表
        changeProject.setCheckStatus(checkStatus);
        if("xt".equals(type)){
            changeProject.setCheckBy("admin");
        }else{
            changeProject.setCheckBy(CommonUtil.getUserId());
        }

        changeProject.setCheckDate(DateUtil.getCurrentDateString());
        supplierProjectManagerChangeService.updateByPrimaryKey(changeProject);


    }

    // 已经发布的项目生成变更记录
    public Boolean addChangeRecord(SupplierProject oldObject, SupplierProjectChange newObject) throws Exception {
        Boolean isbg = false;
        //遍历所有的字段,然后动态调用get方法
        Class clazz = oldObject.getClass();
        Class newclazz = newObject.getClass();
        //获取object的所有属性
        PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz, Object.class).getPropertyDescriptors();
        PropertyDescriptor[] newpds = Introspector.getBeanInfo(newclazz, Object.class).getPropertyDescriptors();

        for (PropertyDescriptor pd : pds) {
            String fieldName = pd.getName();
            //遍历获取属性名
            Mean mean = getMeanByFieldName(clazz, pd.getName());
            if (mean != null) {
                Method readMethod = pd.getReadMethod();
                // 在oldObject上调用get方法等同于获得oldObject的属性值
                Object oldValue = readMethod.invoke(oldObject);
                PropertyDescriptor fieldNew = getField(newpds, fieldName);
                Object newValue = null;
                if (fieldNew != null) {
                    Method readMethodnew = fieldNew.getReadMethod();
                    newValue = readMethodnew.invoke(newObject);
                }
                if("项目归属".equals(mean.value())){
                    oldValue = supervisorProjectMapper.queryXmgs((String)oldValue);
                    newValue = supervisorProjectMapper.queryXmgs((String)newValue);
                }
                if (oldValue == null && newValue == null) {
                    continue;
                } else if ((newValue != null &&  oldValue!=null &&  (!oldValue.equals(newValue))) || (newValue == null && oldValue != null)) {
                    SupervisorProjectModifiedRecord modifiedRecord = new SupervisorProjectModifiedRecord();
                    modifiedRecord.setSpmrId(IdUtil.simpleUUID());
                    modifiedRecord.setSpField(pd.getName());
                    modifiedRecord.setSpFieldName(mean.value());
                    modifiedRecord.setSpmrModifiedTime(DateUtil.getCurrentDateTimeString());
                    modifiedRecord.setSpmrAction("修改");
                    System.out.println("測hi000000000");
                    updatedofiled(modifiedRecord, fieldName, oldValue, newValue);
                    modifiedRecord.setSpId(newObject.getSupProjectChangeId());
                    supervisorProjectModifiedRecordService.insertSelective(modifiedRecord);
                    isbg = true;

                }else if (oldValue==null && newValue!=null &&  (!newValue.equals(oldValue)) ) {
                    SupervisorProjectModifiedRecord modifiedRecord = new SupervisorProjectModifiedRecord();
                    modifiedRecord.setSpmrId(IdUtil.simpleUUID());
                    modifiedRecord.setSpField(pd.getName());
                    modifiedRecord.setSpFieldName(mean.value());
                    modifiedRecord.setSpmrModifiedTime(DateUtil.getCurrentDateTimeString());
                    modifiedRecord.setSpmrAction("修改");
                    System.out.println("測hi000000000");
                    updatedofiled(modifiedRecord, fieldName, oldValue, newValue);
                    modifiedRecord.setSpId(newObject.getSupProjectChangeId());
                    supervisorProjectModifiedRecordService.insertSelective(modifiedRecord);
                    isbg = true;

                }

            }


        }
        //实施地点变更
        Boolean palaceChange = updatedoPlaceChange(oldObject, newObject);
        if(palaceChange){
            isbg = true;
        }


        return isbg;

    }


    public void updatedofiled(SupervisorProjectModifiedRecord modifiedRecord, String fieldName, Object oldValue, Object newValue)  throws  Exception {
        System.out.println("測9999990");
        System.out.println(fieldName + "::::::::::::::::::::::::::::");

        if (fieldName.equals("needZb") || fieldName.equals("isZb") || fieldName.equals("qdHt") || fieldName.equals("isSfk") || fieldName.equals("isGk")) {
            if ("1".equals(oldValue + "")) {
                modifiedRecord.setOldValue("是");
            } else if ("0".equals(oldValue + "")) {
                modifiedRecord.setOldValue("否");
            } else {
                modifiedRecord.setOldValue("");
            }
            if ("1".equals(newValue + "")) {
                modifiedRecord.setNewValue("是");
            } else if ("0".equals(newValue + "")) {
                modifiedRecord.setNewValue("否");
            } else {
                modifiedRecord.setNewValue("");
            }
        } else if (fieldName.equals("htSignProvince") || fieldName.equals("htSignCity") || fieldName.equals("htSignCounty")) {
            SysRegion oldregion = sysRegionService.selectRegionBycode(oldValue + "");
            SysRegion newregion = sysRegionService.selectRegionBycode(newValue + "");
            modifiedRecord.setOldValue(oldregion.getName());
            modifiedRecord.setNewValue(newregion.getName());

        } else if (fieldName.equals("mainFzTrip")) {
            if ("1".equals(oldValue + "")) {
                modifiedRecord.setOldValue("是");
            } else if ("0".equals(oldValue + "")) {
                modifiedRecord.setOldValue("否");
            } else {
                modifiedRecord.setOldValue("否");
            }
            if ("1".equals(newValue + "")) {
                modifiedRecord.setNewValue("是");
            } else if ("0".equals(newValue + "")) {
                modifiedRecord.setNewValue("否");
            } else {
                modifiedRecord.setNewValue("否");
            }

        } else if (fieldName.equals("mainFzSex")) {
            if ("1".equals(oldValue + "")) {
                modifiedRecord.setOldValue("男");
            } else if ("0".equals(oldValue + "")) {
                modifiedRecord.setOldValue("女");
            } else {
                modifiedRecord.setOldValue("");
            }
            if ("1".equals(newValue + "")) {
                modifiedRecord.setNewValue("男");
            } else if ("0".equals(newValue + "")) {
                modifiedRecord.setNewValue("女");
            } else {
                modifiedRecord.setNewValue("");
            }

        } else if (fieldName.equals("mainFzField")) {
            if (oldValue != null && !"".equals(oldValue)) {
                String hyTypestr = getLxText(oldValue + "", DictTypes.DOBEST.getType());
                modifiedRecord.setOldValue(hyTypestr);
            } else {
                modifiedRecord.setOldValue("");
            }
            if (newValue != null && !"".equals(newValue)) {
                String hyTypestr = getLxText(newValue + "", DictTypes.DOBEST.getType());
                modifiedRecord.setNewValue(hyTypestr);
            } else {
                modifiedRecord.setNewValue("");
            }
        } else if (fieldName.equals("projectType")) {
            if (oldValue != null && !"".equals(oldValue)) {
                String hyTypestr = getLxText(oldValue + "", DictTypes.YW_TYPE.getType());
                modifiedRecord.setOldValue(hyTypestr);
            } else {
                modifiedRecord.setOldValue("");
            }
            if (newValue != null && !"".equals(newValue)) {
                String hyTypestr = getLxText(newValue + "", DictTypes.YW_TYPE.getType());
                modifiedRecord.setNewValue(hyTypestr);
            } else {
                modifiedRecord.setNewValue("");
            }
        } else if (fieldName.equals("hyType")) {
            if (oldValue != null && !"".equals(oldValue)) {
                String hyTypestr = getLxText(oldValue + "", DictTypes.INDUSTRY.getType());
                modifiedRecord.setOldValue(hyTypestr);
            } else {
                modifiedRecord.setOldValue("");
            }
            if (newValue != null && !"".equals(newValue)) {
                String hyTypestr = getLxText(newValue + "", DictTypes.INDUSTRY.getType());
                modifiedRecord.setNewValue(hyTypestr);
            } else {
                modifiedRecord.setNewValue("");
            }

        } else if (fieldName.equals("mainFzXl")) {
            // 学历类型
            if (oldValue != null && !"".equals(oldValue)) {
                BaseDict AoBaseDict = DictUtil.getDictByCode(DictTypes.EDUCATION, oldValue + "");
                modifiedRecord.setOldValue(AoBaseDict.getBadtName());
            } else {
                modifiedRecord.setOldValue("");
            }
            if (newValue != null && !"".equals(newValue)) {
                BaseDict AoBaseDict = DictUtil.getDictByCode(DictTypes.EDUCATION, newValue + "");
                modifiedRecord.setNewValue(AoBaseDict.getBadtName());
            } else {
                modifiedRecord.setNewValue("");
            }
        } else {
            if (oldValue != null && !"".equals(oldValue)) {
                modifiedRecord.setOldValue(oldValue.toString());
            } else {
                modifiedRecord.setOldValue("");
            }
            if (newValue != null && !"".equals(newValue)) {
                modifiedRecord.setNewValue(newValue.toString());
            } else {
                modifiedRecord.setNewValue("");
            }
        }


    }

    public String getLxText(String field, String typecode) {
        List<BaseDict> codeList = baseDictService.selectByTypeCode(typecode);
        HashMap<String, String> mapbest = new HashMap();
        for (int i = 0; i < codeList.size(); i++) {
            mapbest.put(codeList.get(i).getBadtCode(), codeList.get(i).getBadtName());
        }
        //擅长领域
        List<String> dobest = StringUtil.split(field);
        List<String> best = new ArrayList<>();
        for (int i = 0; i < dobest.size(); i++) {
            String s = dobest.get(i);
            String scname = mapbest.get(s);
            best.add(scname);
        }
        String strbest = best.toString().replaceAll("\\[", "").replaceAll("\\]", "");
        return strbest;

    }

    /**
     * @Author:zhaoyongke
     * @Description:
     * @param oldObject
     * @param newObject
     * @Date:11:21 2021/4/19
     */
    public Boolean updatedoPlaceChange(SupplierProject oldObject, SupplierProjectChange newObject)  throws  Exception{
        Boolean  isPlaceChange=false;
        //新的实施地点结果集
        ProjectPlaceChangeDTO  dto=new ProjectPlaceChangeDTO();
        dto.setType("001");//实施地点
        dto.setSupProjectId(oldObject.getSupProjectId());
        dto.setSupProjectChangeId(newObject.getSupProjectChangeId());
        List<ProjectPlaceChange> selectNewPlace = projectPlaceChangeService.select(dto);

        //旧的实施地点结果集
        ProjectPlaceDTO  dtoOLd=new ProjectPlaceDTO();
        dtoOLd.setType("001");//实施地点
        dtoOLd.setSupProjectId(oldObject.getSupProjectId());
        List<ProjectPlace> selectOLdPlace = projectPlaceService.select(dtoOLd);
        // 判断有没有变更
        Boolean changePlace = updateisChangePlace(oldObject, selectNewPlace,"001");

        if(changePlace){
            isPlaceChange=true;
            String newvalue = getvalueStrChangePlace(selectNewPlace);
            String oldvalue = getvalueStrOldPlace(selectOLdPlace);
            SupervisorProjectModifiedRecord modifiedRecord = new SupervisorProjectModifiedRecord();
            modifiedRecord.setSpmrId(IdUtil.simpleUUID());
            modifiedRecord.setSpField("实施地点");
            modifiedRecord.setSpFieldName("实施地点");
            modifiedRecord.setSpmrModifiedTime(DateUtil.getCurrentDateTimeString());
            modifiedRecord.setSpmrAction("修改");
            System.out.println("測hi000000000");
            modifiedRecord.setOldValue(oldvalue);
            modifiedRecord.setNewValue(newvalue);
            modifiedRecord.setSpId(newObject.getSupProjectChangeId());
            supervisorProjectModifiedRecordService.insertSelective(modifiedRecord);
        }
        //主要负责人要求地区
        ProjectPlaceChangeDTO  dtoperson=new ProjectPlaceChangeDTO();
        dtoperson.setType("002");//实施地点
        dtoperson.setSupProjectId(oldObject.getSupProjectId());
        dtoperson.setSupProjectChangeId(newObject.getSupProjectChangeId());
        List<ProjectPlaceChange> selectNewPersonPlace = projectPlaceChangeService.select(dtoperson);
        //旧的实施地点结果集
        ProjectPlaceDTO dtoOLdPerson=new ProjectPlaceDTO();
        dtoOLdPerson.setType("002");//实施地点
        dtoOLdPerson.setSupProjectId(oldObject.getSupProjectId());
        List<ProjectPlace> selectOLdPersonPlace = projectPlaceService.select(dtoOLdPerson);
        // 判断有没有变更
        Boolean changePersonPlace = updateisChangePlace(oldObject, selectNewPersonPlace,"002");
        if(changePersonPlace){
            isPlaceChange=true;
            String newvalue = getvalueStrChangePlace(selectNewPersonPlace);
            String oldvalue = getvalueStrOldPlace(selectOLdPersonPlace);
            SupervisorProjectModifiedRecord modifiedRecord = new SupervisorProjectModifiedRecord();
            modifiedRecord.setSpmrId(IdUtil.simpleUUID());
            modifiedRecord.setSpField("主要负责人地区要求");
            modifiedRecord.setSpFieldName("主要负责人地区要求");
            modifiedRecord.setSpmrModifiedTime(DateUtil.getCurrentDateTimeString());
            modifiedRecord.setSpmrAction("修改");
            System.out.println("測hi000000000");
            modifiedRecord.setOldValue(oldvalue);
            modifiedRecord.setNewValue(newvalue);
            modifiedRecord.setSpId(newObject.getSupProjectChangeId());
            supervisorProjectModifiedRecordService.insertSelective(modifiedRecord);
        }



        return  isPlaceChange;

    }


    /**
     * 通过字段名称获取Mean注解值
     *
     * @param clazz
     * @param name
     */
    private Mean getMeanByFieldName(Class clazz, String name) {
        Field field = null;
        try {
            field = clazz.getDeclaredField(name);
        } catch (NoSuchFieldException e) {
            //没有属性，去父类中找：
            try {
                field = clazz.getSuperclass().getDeclaredField(name);
            } catch (NoSuchFieldException ne) {

            }
        }
        return field.getAnnotation(Mean.class);
    }

    /**
     * @Author:zhaoyongke
     * @Description:
     * @param oldObject
     * @param selectNewPlace
     * @Date:11:20 2021/4/19
     */
    public  Boolean  updateisChangePlace(SupplierProject oldObject, List<ProjectPlaceChange> selectNewPlace,String  type) throws  Exception {
        Boolean  isChange=false;
        for (int i = 0; i <selectNewPlace.size() ; i++) {
            ProjectPlaceChange projectPlaceChange = selectNewPlace.get(i);
            String province = projectPlaceChange.getProvince();
            String city = projectPlaceChange.getCity();
            String county = projectPlaceChange.getCounty();
            ProjectPlace  place=new ProjectPlace();
            place.setType(type);
            place.setProvince(province);
            place.setCity(city);
            place.setCounty(county);
            place.setSupProjectId(oldObject.getSupProjectId());
            int i1 = projectPlaceService.selectCount(place);
            if(i1==0){
                isChange=true;
                break;
            }
        }
        return  isChange;
    }




    /**
     * @Author:zhaoyongke
     * @Description:
     * @param selectPlace
     * @Date:11:20 2021/4/19
     */
    public  String  getvalueStrChangePlace( List<ProjectPlaceChange> selectPlace){
        String  newvalue="";
        for (int i = 0; i <selectPlace.size() ; i++) {
            ProjectPlaceChange projectPlaceChange = selectPlace.get(i);
            String province = projectPlaceChange.getProvince();
            String city = projectPlaceChange.getCity();
            String county = projectPlaceChange.getCounty();
            String provincename="";
            String cityname="";
            String countyname="";
            if(StringUtil.isNotBlank(province)){
                SysRegion provincedto = sysRegionService.selectRegionBycode(province + "");
                if(provincedto!=null){
                    provincename = provincedto.getName();
                }
            }
            if(StringUtil.isNotBlank(province)){
                SysRegion citydto = sysRegionService.selectRegionBycode(city + "");
                if(citydto!=null){
                    cityname = citydto.getName();
                }
            }
            if(StringUtil.isNotBlank(county)){
                SysRegion countydto = sysRegionService.selectRegionBycode(county + "");
                if(countydto!=null){
                    countyname = countydto.getName();
                }
            }
            String  str=provincename+cityname+countyname;
            newvalue=newvalue+str+",";
        }
        if(StringUtil.isNotBlank(newvalue)){
            newvalue=newvalue.substring(0,newvalue.length()-1);

        }

        return  newvalue;


    }

    public PropertyDescriptor getField(PropertyDescriptor[] newpds, String fieldName) {
        PropertyDescriptor pdre = null;
        for (PropertyDescriptor pd : newpds) {
            if (fieldName.equals(pd.getName())) {
                pdre = pd;
                return pdre;
            }
        }
        return pdre;
    }

    /**
     * @Author:zhaoyongke
     * @Description:
     * @param selectPlace
     * @Date:11:20 2021/4/19
     */
    public  String  getvalueStrOldPlace( List<ProjectPlace> selectPlace) throws  Exception {
        String  newvalue="";
        for (int i = 0; i <selectPlace.size() ; i++) {
            ProjectPlace projectPlace = selectPlace.get(i);
            String province = projectPlace.getProvince();
            String city = projectPlace.getCity();
            String county = projectPlace.getCounty();
            String provincename="";
            String cityname="";
            String countyname="";
            if(StringUtil.isNotBlank(province)){
                SysRegion provincedto = sysRegionService.selectRegionBycode(province + "");
                if(provincedto!=null){
                    provincename = provincedto.getName();
                }
            }
            if(StringUtil.isNotBlank(province)){
                SysRegion citydto = sysRegionService.selectRegionBycode(city + "");
                if(citydto!=null){
                    cityname = citydto.getName();
                }
            }
            if(StringUtil.isNotBlank(county)){
                SysRegion countydto = sysRegionService.selectRegionBycode(county + "");
                if(countydto!=null){
                    countyname = countydto.getName();
                }
            }
            String  str=provincename+cityname+countyname;
            newvalue=newvalue+str+",";
        }
        if(StringUtil.isNotBlank(newvalue)){
            newvalue=newvalue.substring(0,newvalue.length()-1);

        }

        return  newvalue;


    }


    /**
     * @Author:zhaoyongke
     * @Description:项目变更给应接人发送消息
     * @Date:11:38 2021/4/8
     */
    public void sendNoticeToYjByChange(SupplierProject project, SupplierProjectChangeDTO changeProject) {
        SupplierProjectAccept accept = new SupplierProjectAccept();
        accept.setSupProjectId(project.getSupProjectId());
        List<SupplierProjectAccept> select = supplierProjectAcceptService.select(accept);
        for (int i = 0; i < select.size(); i++) {
            SupplierProjectAccept accept1 = select.get(i);
            String acceptBy = accept1.getAcceptBy();
            sendNoticeChange(acceptBy, project, changeProject);
        }


    }

    /**
     * @param acceptBy
     * @param project
     * @Author:zhaoyongke
     * @Description:项目变更发送消息
     * @Date:11:38 2021/4/8
     */
    public void sendNoticeChange(String acceptBy, SupplierProject project, SupplierProjectChangeDTO supplierProjectChangeDTO) {
        String currdate = DateUtil.getCurrentDateString("yyyyMMdd");
        ProjectNotice notice = new ProjectNotice();
        notice.setLinkId(supplierProjectChangeDTO.getSupProjectChangeId());
        notice.setIsView("0");
        notice.setSendTo(acceptBy);
        notice.setTitle(project.getProjectName() + "项目变更公告（" + currdate + "）版，请及时查看");
        notice.setNoticeType("102");// 公示结束 发送消息
        notice.setApplicantType("002");// 供需
        notice.setCreateDate(DateUtil.getCurrentDateString());
        projectNoticeServie.insertSelective(notice);
    }













}
